<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    function Promise(executor){

        //保存回调函数 队列
        this.callbacks=[];

        this.PromiseState='pending';
        this.PromiseResult=null;

        const self=this;

        //成功回调函数
        function resolve(data){
            //状态只能修改一次
            if(self.PromiseState!=='pending') return;

            self.PromiseState='fulfilled';
            self.PromiseResult=data;

            //调用回调函数 队列
            self.callbacks.forEach(function (item){
                item.onResolved(data)
            })
        }

        //失败回调函数
        function reject(data){
            if(self.PromiseState!=='pending') return;
            self.PromiseState='rejected';
            self.PromiseResult=data;


            //调用回调函数  队列
          self.callbacks.forEach(function (item){
              item.onRejected(data)
          })


        }


        //捕捉异常
        try {
            //同步调用 执行器函数
            executor(resolve, reject);
        } catch (e) {
            reject(e)
        }
    }

    Promise.prototype.then= function (onResolved,onRejected){

        const self=this;

   //     console.log(self)

        return new Promise( (resolve,reject) => {

            //回调函数
            /*  onResolved=  function (value){
                     console.log(value)
                                    }*/
            if(this.PromiseState==='fulfilled'){

                try{
                    let result= onResolved(self.PromiseResult);  //什么时候会出现结果是promise对象?  比如190行可以return

                    if(result instanceof Promise){        //这里其实用到了递归
                        //如果结果是promise对象
                        result.then(success => {        //拿到promise对象回调的结果
                            resolve(success)             //then方法就是来接受调用他的promise对象的结果
                        }, fail => {
                            reject(fail)
                        })
                    }else{
                        //如果结果是状态
                        resolve(result)
                    }
                }catch (e){
                    reject(e)
                }

            }


            if(this.PromiseState==='rejected'){


                try{
                    let result= onRejected(self.PromiseResult);

                    if(result instanceof Promise){
                        //如果结果是promise对象
                        result.then(success => {
                            resolve(success)
                        }, fail => {
                            reject(fail)
                        })
                    }else{
                        //如果结果是状态
                        resolve(result)
                    }
                }catch (e){
                    reject(e)
                }
            }



            //保存回调函数 队列
            if(this.PromiseState==='pending'){
                this.callbacks.push({
                    //保存异步成功的回调函数
                    'onResolved':function (){

                        try{
                            let result= onResolved(self.PromiseResult);

                            if(result instanceof Promise){
                                //如果结果是promise对象
                                result.then(success => {
                                    resolve(success)
                                }, fail => {
                                    reject(fail)
                                })
                            }else{
                                //如果结果是状态
                                resolve(result)
                            }
                        }catch (e){
                            reject(e)
                        }
                    },

                    //保存异步失败的回调函数
                    'onRejected':function (){
                        try{
                            let result= onRejected(self.PromiseResult);

                            if(result instanceof Promise){
                                //如果结果是promise对象
                                result.then(success => {
                                    resolve(success)
                                }, fail => {
                                    reject(fail)
                                })
                            }else{
                                //如果结果是状态
                                resolve(result)
                            }
                        }catch (e){
                            reject(e)
                        }
                    }
                })
            }


        })



    }



    //所谓的成功和失败 只是我们定义的两种状态
    let p=new Promise((resolve,reject)=>{

        setTimeout(function (){
         //   resolve('ok');
            reject('error')
        },2000)

    });

    //后续的then 要拿到前面then 执行的结果  成功和失败回调的结果传递给下一个 成功回调函数   只有异常的结果会传递给  失败回调函数
    p.then(success => {               //第一个then 因为调用它的promise 状态是reject   所以调用fail回调函数并传递结果'error'
        console.log(success)
        return '123'
    },fail => {
        console.log(fail)
        return new Promise(

          (resolve,reject)=>{

              setTimeout(function (){
                  //   resolve('ok');
                  resolve('456')
              },2000)

          }
      )

    }).then(success => {       //第二个then  因为调用它的promise 状态是 resolve  所以调用success  并传递结果 '456'
        console.log(success)
       throw '错误'
    },fail =>{

    }).then(success => {      //第三个then  因为调用它的promise中抛出了异常 所以状态是 reject   调用fail 并传递结果 '错误'
        console.log(success)
        return '789'
    },fail =>{
        console.log(fail)
        return '789'
    }).then(success => {      //第四个then  状态是 resolve   调用success 并传递结果 '789'
        console.log(success)

    },fail =>{
     
    })


</script>
</body>
</html>